export default ({
  namespaced:true,
  state: {
    cartList:[]
  },
  mutations: {
    change_cartList (start, data) {
      let addFlag = true;//新增状态
      let cartList_ls = this.state.cartStore.cartList;//购物车列表
      // console.log(cartList_ls);
      // console.log(data);
      cartList_ls.forEach(item => {
        if (item.title == data.title) {//判断购物车里有没有
          console.log('购物车已经有了');
          addFlag = false;
          item.count++;
        }
      });
      if (addFlag) {
        console.log('加入购物车');
        data.count = 1;
        cartList_ls.push(data);
      }
    },
    change_cartList_count (start, data) {
      console.log(data);
      
      start.cartList.map((item,index) => {
        if (data.num == 2) {
          if (item.title == data.title) {
            item.count++
          }
        } else {
          if (item.title == data.title) {
            if (item.count > 1) {
              item.count--
            } else {
              start.cartList.splice(index,1)
            }
          }
        }
      })
    },
  },
  actions: {
    set_change_cartList ({commit},data) {//加入购物车
      commit('change_cartList',data)
    },
    set_change_cartList_count ({commit},data,num) {//购物车数量增加
      commit('change_cartList_count',data)
    },
  }
})
